<template>
  <div class="sidebar">
    <div class="sidebar-section">
      <h3>推荐音乐</h3>
      <ul class="menu-list">
        <li @click="$emit('load-daily-recommend')" :class="{ active: activeMenu === 'daily' }">
          <i class="icon">🎵</i>每日推荐
        </li>
        <li @click="$emit('load-fm')" :class="{ active: activeMenu === 'fm' }">
          <i class="icon">📻</i>私人FM
        </li>
        <li @click="$emit('load-hot-songs')" :class="{ active: activeMenu === 'hot' }">
          <i class="icon">🔥</i>热门歌曲
        </li>
      </ul>
    </div>

    <div class="sidebar-section">
      <h3>我的音乐</h3>
      <ul class="menu-list" v-if="user">
        <li @click="$emit('load-liked-songs')" :class="{ active: activeMenu === 'liked' }">
          <i class="icon">❤️</i>我喜欢的音乐
        </li>
        <li @click="$emit('load-recent-play')" :class="{ active: activeMenu === 'recent' }">
          <i class="icon">🕒</i>最近播放
        </li>
        <li @click="$emit('load-cloud-music')" :class="{ active: activeMenu === 'cloud' }">
          <i class="icon">☁️</i>我的音乐云盘
        </li>
      </ul>
      <div v-else class="login-prompt">
        <p>登录后查看个人音乐</p>
      </div>
    </div>

    <div class="sidebar-section">
      <div class="section-header">
        <h3>创建的歌单</h3>
        <button v-if="user" @click="$emit('create-playlist')" class="create-btn">+</button>
      </div>
      <ul class="playlist-list" v-if="userPlaylists.length > 0">
        <li 
          v-for="playlist in userPlaylists" 
          :key="playlist.id"
          @click="$emit('load-playlist', playlist.id)"
          :class="{ active: currentPlaylist === playlist.id }"
        >
          <img :src="playlist.coverImgUrl" :alt="playlist.name" />
          <span>{{ playlist.name }}</span>
          <span class="track-count">{{ playlist.trackCount }}</span>
        </li>
      </ul>
    </div>

    <div class="sidebar-section">
      <h3>推荐歌单</h3>
      <ul class="playlist-list">
        <li 
          v-for="playlist in recommendPlaylists" 
          :key="playlist.id"
          @click="$emit('load-playlist', playlist.id)"
          :class="{ active: currentPlaylist === playlist.id }"
        >
          <img :src="playlist.coverImgUrl" :alt="playlist.name" />
          <span>{{ playlist.name }}</span>
          <span class="play-count">{{ formatPlayCount(playlist.playCount) }}</span>
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Sidebar',
  props: {
    user: {
      type: Object,
      default: null
    },
    recommendPlaylists: {
      type: Array,
      default: () => []
    },
    userPlaylists: {
      type: Array,
      default: () => []
    },
    currentPlaylist: {
      type: [String, Number],
      default: null
    },
    activeMenu: {
      type: String,
      default: ''
    }
  },
  methods: {
    formatPlayCount(count) {
      if (count > 100000000) {
        return Math.floor(count / 100000000) + '亿';
      } else if (count > 10000) {
        return Math.floor(count / 10000) + '万';
      }
      return count;
    }
  }
};
</script>

<style scoped>
.sidebar {
  width: 250px;
  background-color: #f8f8f8;
  padding: 20px 0;
  overflow-y: auto;
  height: 100%;
  border-right: 1px solid #e0e0e0;
}

.sidebar-section {
  margin-bottom: 30px;
  padding: 0 20px;
}

.sidebar-section h3 {
  font-size: 16px;
  color: #333;
  margin-bottom: 15px;
  font-weight: 600;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.create-btn {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid #ccc;
  background: white;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.create-btn:hover {
  background-color: #f0f0f0;
}

.menu-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.menu-list li {
  display: flex;
  align-items: center;
  padding: 10px 15px;
  cursor: pointer;
  border-radius: 8px;
  margin-bottom: 5px;
  font-size: 14px;
  color: #666;
}

.menu-list li:hover {
  background-color: #f0f0f0;
  color: #333;
}

.menu-list li.active {
  background-color: #e6f7ff;
  color: #1890ff;
}

.menu-list li .icon {
  margin-right: 10px;
  font-size: 16px;
}

.playlist-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.playlist-list li {
  display: flex;
  align-items: center;
  padding: 8px 15px;
  cursor: pointer;
  border-radius: 8px;
  margin-bottom: 5px;
}

.playlist-list li:hover {
  background-color: #f0f0f0;
}

.playlist-list li.active {
  background-color: #e6f7ff;
}

.playlist-list li img {
  width: 40px;
  height: 40px;
  border-radius: 6px;
  margin-right: 12px;
  flex-shrink: 0;
}

.playlist-list li span {
  flex: 1;
  font-size: 13px;
  color: #333;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.track-count, .play-count {
  font-size: 11px;
  color: #999;
  margin-left: 8px;
  flex-shrink: 0;
}

.login-prompt {
  text-align: center;
  padding: 20px;
  color: #999;
  font-size: 14px;
}
</style>